/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Hang.java
 *
 * Created on Dec 1, 2011, 11:18:10 PM
 */
package GUI;
import Bussiness.ChungLoai;
import java.util.Vector;
import javax.swing.table.DefaultTableModel;
import Bussiness.HangHoa;
import java.sql.SQLException;
import javax.swing.JOptionPane;
import java.lang.String;

/**
 *
 * @author Nlanh_BN
 */
public class FrmHangHoa extends javax.swing.JFrame {

    static String bot;
   
    private DefaultTableModel modelTable = new DefaultTableModel();
    /** Creates new form NCC */
    public FrmHangHoa() {
        initComponents();
        bot = "";
        this.loadCombo();
        this.display();
        this.teststatus();
    }
    
    public void search(){
        HangHoa b=new HangHoa();
        Vector v=new Vector();        
        try {
            if (cboSearch.getSelectedIndex()==0)
                v=b.getHangHoa_MaHang(txtSearch.getText());
            else
                 if (cboSearch.getSelectedIndex()==1)
                v=b.getHangHoa_TenHang(txtSearch.getText());
            else
                v=b.getHangHoa_Chungloai(txtSearch.getText());    
            
        } catch (SQLException e) {
            e.printStackTrace();
        }  
          this.loadTable(v);
    }
     public void update()
    {             
        HangHoa c = new HangHoa();
        c.setMahang(txtMaHang.getText());
        c.setTenhang(txtTenHang.getText());
        c.setChungloai(cboChungLoai.getSelectedItem().toString());
        c.setDongia(Float.parseFloat(txtDonGia.getText()));       
        c.setDonvitinh(txtDvTinh.getText());
        c.setHSD(txtHanSuDung.getText());
        c.setSoluong(Integer.parseInt(txtSoLuong.getText()));
        int t = 0;
        try{
            t = c.updateHangHoa(c);
        }catch(SQLException e){
            e.printStackTrace();
        }
        if(t==1)
            JOptionPane.showMessageDialog(this,"UPdate successful","Infofrm",JOptionPane.INFORMATION_MESSAGE);
        else
            JOptionPane.showMessageDialog(this,"Update erro!");
     
    }
     public void loadTable(Vector v)
    {
        modelTable.setNumRows(0);
        Vector v1 = new Vector();
        v1.addElement("Mã Hàng");
        v1.addElement("Tên Hàng");
        v1.addElement("Giá Bán");
        v1.addElement("Đơn vị tính");
        v1.addElement("Chủng loại");
        v1.addElement("Hạn sử dụng");
        v1.addElement("Số lượng");
        
        modelTable.setColumnIdentifiers(v1);
        HangHoa b = new HangHoa();
        for (int i = 0; i < v.size(); i++) {
            b = (HangHoa)v.get(i);
            Vector row = new Vector();
            row.addElement(b.getMahang());
            row.addElement(b.getTenhang());
            row.addElement(b.getChungloai());
            row.addElement(b.getDongia());
            row.addElement(b.getDonvitinh());
            row.addElement(b.getHSD());
            row.addElement(b.getSoluong());
            modelTable.addRow(row);
        }
        tblHangHoa.setModel(modelTable);
        int r = tblHangHoa.getSelectedRow();
        if(r < 0){
            this.bot = "";
            btnEdit.setEnabled(false);
            btnDelete.setEnabled(false);
            return;
        }
        else{
            bot = tblHangHoa.getValueAt(r, 0).toString();
        }
    }
     public void loadCombo() {
        HangHoa b = new HangHoa();
        ChungLoai a = new ChungLoai();
        cboChungLoai.removeAllItems();
        try {
            Vector v = a.getAllChungLoai();

            for (int i = 0; i < v.size(); i++) {
                cboChungLoai.addItem(((ChungLoai) v.get(i)).getMachungloai());
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }
      public void display()
    {
        HangHoa b1 = new HangHoa();
        Vector v1 = new Vector();
        try {
           v1=b1.getAllHangHoa(); 
        } catch (SQLException e) {
            e.printStackTrace();
        }
        this.loadTable(v1);
    }
      public void teststatus()
    {
        if(Login.status == "nhanvien"){
           btnEdit.setEnabled(false);
           btnDelete.setEnabled(false);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        txtMaHang = new javax.swing.JTextField();
        txtTenHang = new javax.swing.JTextField();
        txtHanSuDung = new javax.swing.JTextField();
        txtSoLuong = new javax.swing.JTextField();
        txtDvTinh = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblHangHoa = new javax.swing.JTable();
        btnLoad = new javax.swing.JButton();
        btnAdd = new javax.swing.JButton();
        btnEdit = new javax.swing.JButton();
        btnDelete = new javax.swing.JButton();
        jLabel7 = new javax.swing.JLabel();
        btnSearch = new javax.swing.JButton();
        txtSearch = new javax.swing.JTextField();
        cboSearch = new javax.swing.JComboBox();
        jLabel8 = new javax.swing.JLabel();
        txtDonGia = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        cboChungLoai = new javax.swing.JComboBox();
        jLabel10 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("Mã Hàng");

        jLabel2.setText("Tên Hàng");

        jLabel3.setText("DV tính");

        jLabel4.setText("Chủng Loại");

        jLabel5.setText("Hạn Sử Dụng");

        jLabel6.setText("Số Lượng");

        txtHanSuDung.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtHanSuDungActionPerformed(evt);
            }
        });

        tblHangHoa.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tblHangHoa.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblHangHoaMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tblHangHoa);

        btnLoad.setText("Load");
        btnLoad.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLoadActionPerformed(evt);
            }
        });

        btnAdd.setText("Add");
        btnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddActionPerformed(evt);
            }
        });

        btnEdit.setText("Edit");
        btnEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditActionPerformed(evt);
            }
        });

        btnDelete.setText("Delete");

        jLabel7.setText("Tìm theo: ");

        btnSearch.setText("Search");
        btnSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSearchActionPerformed(evt);
            }
        });

        cboSearch.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Mahang", "Tenhang", "Chungloai" }));

        jLabel8.setText("Giá bán");

        jLabel9.setText("VND");

        cboChungLoai.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "loai1", "loai2", "loai3", "loai4", "loai5" }));

        jLabel10.setText("QUẢN LÝ THÔNG TIN HÀNG HÓA ");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(273, 273, 273)
                .addComponent(jLabel10)
                .addContainerGap(181, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(btnSearch)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel7)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(cboSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 578, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(jLabel2)
                                .addComponent(jLabel1))
                            .addComponent(jLabel8)
                            .addComponent(btnAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtMaHang, javax.swing.GroupLayout.DEFAULT_SIZE, 129, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addComponent(txtDonGia, javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(txtTenHang, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 99, Short.MAX_VALUE)
                                    .addComponent(btnEdit, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jLabel9)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel4)
                                    .addComponent(jLabel3))
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(txtDvTinh, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(cboChungLoai, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel5)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(41, 41, 41)
                                        .addComponent(jLabel6)))
                                .addGap(20, 20, 20)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(txtSoLuong)
                                    .addComponent(txtHanSuDung, javax.swing.GroupLayout.DEFAULT_SIZE, 81, Short.MAX_VALUE)))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(btnLoad, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(50, 50, 50)
                                .addComponent(btnDelete, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addGap(26, 26, 26))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel10)
                        .addGap(31, 31, 31)
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(txtTenHang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel4)
                            .addComponent(cboChungLoai, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel6))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel8)
                            .addComponent(txtDonGia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel9)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(43, 43, 43)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtDvTinh, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3)
                            .addComponent(txtMaHang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel5)
                            .addComponent(txtHanSuDung, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addComponent(txtSoLuong, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(30, 30, 30)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnAdd)
                    .addComponent(btnEdit)
                    .addComponent(btnLoad)
                    .addComponent(btnDelete))
                .addGap(28, 28, 28)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 182, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(cboSearch, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(49, 49, 49)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel7)
                            .addComponent(txtSearch, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGap(18, 18, 18)
                .addComponent(btnSearch)
                .addContainerGap(21, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_btnAddActionPerformed

private void tblHangHoaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblHangHoaMouseClicked
//TODO add your handling code here:
    int r = tblHangHoa.getSelectedRow();
    if(r < 0){
        bot = "";
        return;
    }else{
        btnEdit.setEnabled(true);
        btnDelete.setEnabled(true);
        bot = tblHangHoa.getValueAt(r, 0).toString();
        
        if (r != -1){
            txtMaHang.setText(String.valueOf(tblHangHoa.getValueAt(r, 0)));
            txtTenHang.setText(String.valueOf(tblHangHoa.getValueAt(r, 1)));
            txtDonGia.setText(String.valueOf(tblHangHoa.getValueAt(r, 2)));
            txtDvTinh.setText(String.valueOf(tblHangHoa.getValueAt(r, 3)));
            cboChungLoai.setSelectedItem(String.valueOf(tblHangHoa.getValueAt(r, 4)));
            txtHanSuDung.setText(String.valueOf(tblHangHoa.getValueAt(r, 5)));
            txtSoLuong.setText(String.valueOf(tblHangHoa.getValueAt(r, 6)));
        }
    }
}//GEN-LAST:event_tblHangHoaMouseClicked

private void btnLoadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLoadActionPerformed
// TODO add your handling code here:
     this.display();
}//GEN-LAST:event_btnLoadActionPerformed

private void btnSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSearchActionPerformed
// TODO add your handling code here:
    this.search();
}//GEN-LAST:event_btnSearchActionPerformed

private void btnEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditActionPerformed
// TODO add your handling code here:
   this.update();
   this.display();
}//GEN-LAST:event_btnEditActionPerformed

private void txtHanSuDungActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtHanSuDungActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_txtHanSuDungActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(FrmHangHoa.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(FrmHangHoa.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(FrmHangHoa.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(FrmHangHoa.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new FrmHangHoa().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAdd;
    private javax.swing.JButton btnDelete;
    private javax.swing.JButton btnEdit;
    private javax.swing.JButton btnLoad;
    private javax.swing.JButton btnSearch;
    private javax.swing.JComboBox cboChungLoai;
    private javax.swing.JComboBox cboSearch;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tblHangHoa;
    private javax.swing.JTextField txtDonGia;
    private javax.swing.JTextField txtDvTinh;
    private javax.swing.JTextField txtHanSuDung;
    private javax.swing.JTextField txtMaHang;
    private javax.swing.JTextField txtSearch;
    private javax.swing.JTextField txtSoLuong;
    private javax.swing.JTextField txtTenHang;
    // End of variables declaration//GEN-END:variables
}
